/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
var sumOfLeftLeaves = function (root) {
    let leftVal = []
    getLeftLeaves(root, false, leftVal)
    return leftVal.reduce((a,b)=>a+b,0)
};

// 确认入参和返回值，无返回值，存储左节点
function getLeftLeaves(root, isLeft, leftVal) {
    // 确认终止条件
    if (root === null) {
        return
    }
    // 确认单次循环逻辑，当是左节点且是叶子结点时push
    if (isLeft && root.left === null && root.right === null) {
        leftVal.push(root.val)
    }

    if (root.left) {
        getLeftLeaves(root.left, true, leftVal)
    }
    if (root.right) {
        getLeftLeaves(root.right, false, leftVal)
    }

}